package br.com.consignum.util;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import br.com.caelum.vraptor.Accepts;
import br.com.caelum.vraptor.AfterCall;
import br.com.caelum.vraptor.AroundCall;
import br.com.caelum.vraptor.BeforeCall;
import br.com.caelum.vraptor.Intercepts;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.interceptor.SimpleInterceptorStack;

@Intercepts(before=ConversationInterceptor.class)
@RequestScoped
public class SessaoValidaInterceptor {

	@Inject
	private HttpServletRequest request;

	@Inject
	private Result requisicao;
	

	@AroundCall
	public void intercept( SimpleInterceptorStack stack ) {
			this.requisicao.include( ParametrosUtil.SESSAO_EXPIRADA, ParametrosUtil.SESSAO_EXPIRADA );
			stack.next();
	}
	@Accepts
	public boolean accepts( ) {
		
		final boolean verificacao = (this.request.getSession( false ).getAttribute( ParametrosUtil.USUARIO_LOGADO ) == null && this.request.getRequestURI().equalsIgnoreCase( "/gl/autenticacao/viewLogin" )
				&& this.request.getParameter( "controleLogin" ) == null );
				
		return verificacao;
	}
	
	@BeforeCall
	public void before() {
	}

	@AfterCall
	public void after() {
	}
}
